import 'package:demo/theme/app_text_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../model/api_list_response.dart';
import '../../../model/order_model.dart';
import '../../../net/api_provider.dart';
import '../../routes/app_pages.dart';

class OrderController extends GetxController {

  final List<Tab> myTabs = <Tab>[
    Tab(height:20.w,text: '全部订单'),
    Tab(height:20.w,text: '审核中'),
    Tab(height:20.w,text: '放款中'),
    Tab(height:20.w,text: '待还款'),
    Tab(height:20.w,text: '已逾期'),
  ];

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  ApiProvider apiProvider = Get.find();

  RefreshController refreshController1 = RefreshController(initialRefresh: true);
  RefreshController refreshController2 = RefreshController(initialRefresh: true);
  RefreshController refreshController3 = RefreshController(initialRefresh: true);
  RefreshController refreshController4 = RefreshController(initialRefresh: true);
  RefreshController refreshController5 = RefreshController(initialRefresh: true);

  var list1 = [].obs;
  var list2 = [].obs;
  var list3 = [].obs;
  var list4 = [].obs;
  var list5 = [].obs;
  var page1 = 1;
  var page2 = 1;
  var page3 = 1;
  var page4 = 1;
  var page5 = 1;
  var canLoadMore1 = false.obs;
  var canLoadMore2 = false.obs;
  var canLoadMore3 = false.obs;
  var canLoadMore4 = false.obs;
  var canLoadMore5 = false.obs;

  void getLoanList1(bool isRefresh) async {
    if(isRefresh){
      page1 = 1;
    }
    var map = {
      'page': page1,
      'page_size' : 20,
      'status' : 0
    };
    var result = await apiProvider.getData(map, ApiProvider.loan_list);
    if(result.code == 200){
      ApiListResponse listResponse = ApiListResponse.fromJson(result.data);
      if(page1 >= listResponse.pageCount){
        canLoadMore1.value = false;
      }else{
        canLoadMore1.value = true;
        page1++;
      }

      List<dynamic> resultList = listResponse.list;
      if(resultList.isNotEmpty){
        List<Order> list = [];
        for(dynamic i in resultList){
          list.add(Order.fromJson(i));
        }
        if(isRefresh){
          list1.value = list;
        }else{
          list1.addAll(list);
        }
      }

      refreshController1.refreshCompleted();
      refreshController1.loadComplete();
    }else{
      EasyLoading.showToast('${result.msg}');
    }
  }

  void getLoanList2(bool isRefresh) async {
    if(isRefresh){
      page2 = 1;
    }
    var map = {
      'page': page2,
      'page_size' : 20,
      'status' : 3
    };
    var result = await apiProvider.getData(map, ApiProvider.loan_list);
    if(result.code == 200){
      ApiListResponse listResponse = ApiListResponse.fromJson(result.data);
      if(page2 >= listResponse.pageCount){
        canLoadMore2.value = false;
      }else{
        canLoadMore2.value = true;
        page2++;
      }

      List<dynamic> resultList = listResponse.list;
      if(resultList.isNotEmpty){
        List<Order> list = [];
        for(dynamic i in resultList){
          list.add(Order.fromJson(i));
        }
        if(isRefresh){
          list2.value = list;
        }else{
          list2.addAll(list);
        }
      }

      refreshController2.refreshCompleted();
      refreshController2.loadComplete();
    }else{
      EasyLoading.showToast('${result.msg}');
    }
  }

  void getLoanList3(bool isRefresh) async {
    if(isRefresh){
      page3 = 1;
    }
    var map = {
      'page': page3,
      'page_size' : 20,
      'status' : 6
    };
    var result = await apiProvider.getData(map, ApiProvider.loan_list);
    if(result.code == 200){
      ApiListResponse listResponse = ApiListResponse.fromJson(result.data);
      if(page3 >= listResponse.pageCount){
        canLoadMore3.value = false;
      }else{
        canLoadMore3.value = true;
        page3++;
      }

      List<dynamic> resultList = listResponse.list;
      if(resultList.isNotEmpty){
        List<Order> list = [];
        for(dynamic i in resultList){
          list.add(Order.fromJson(i));
        }
        if(isRefresh){
          list3.value = list;
        }else{
          list3.addAll(list);
        }
      }

      refreshController3.refreshCompleted();
      refreshController3.loadComplete();
    }else{
      EasyLoading.showToast('${result.msg}');
    }
  }
  void getLoanList4(bool isRefresh) async {
    if(isRefresh){
      page4 = 1;
    }
    var map = {
      'page': page4,
      'page_size' : 20,
      'status' : 9
    };
    var result = await apiProvider.getData(map, ApiProvider.loan_list);
    if(result.code == 200){
      ApiListResponse listResponse = ApiListResponse.fromJson(result.data);
      if(page4 >= listResponse.pageCount){
        canLoadMore4.value = false;
      }else{
        canLoadMore4.value = true;
        page4++;
      }

      List<dynamic> resultList = listResponse.list;
      if(resultList.isNotEmpty){
        List<Order> list = [];
        for(dynamic i in resultList){
          list.add(Order.fromJson(i));
        }
        if(isRefresh){
          list4.value = list;
        }else{
          list4.addAll(list);
        }
      }

      refreshController4.refreshCompleted();
      refreshController4.loadComplete();
    }else{
      EasyLoading.showToast('${result.msg}');
    }
  }
  void getLoanList5(bool isRefresh) async {
    if(isRefresh){
      page5 = 1;
    }
    var map = {
      'page': page5,
      'page_size' : 20,
      'status' : 11
    };
    var result = await apiProvider.getData(map, ApiProvider.loan_list);
    if(result.code == 200){
      ApiListResponse listResponse = ApiListResponse.fromJson(result.data);
      if(page5 >= listResponse.pageCount){
        canLoadMore5.value = false;
      }else{
        canLoadMore5.value = true;
        page5++;
      }

      List<dynamic> resultList = listResponse.list;
      if(resultList.isNotEmpty){
        List<Order> list = [];
        for(dynamic i in resultList){
          list.add(Order.fromJson(i));
        }
        if(isRefresh){
          list5.value = list;
        }else{
          list5.addAll(list);
        }
      }

      refreshController5.refreshCompleted();
      refreshController5.loadComplete();
    }else{
      EasyLoading.showToast('${result.msg}');
    }
  }

  void toDetail(Order order){
    Get.toNamed(Routes.ORDER_DETAIL,arguments: {'loan_num':order.loanNum});
  }
}
